#include <stdio.h>
#include <stdlib.h>


int credits[60];
int grades [60];
char student_f_name[20];
char student_l_name[20];

int getStudent (char id[])
{
    int existance = 0;
    char tempid[8];
    FILE* input;
    input = fopen("Student.txt", "r");
    if(input == NULL)
    {
        printf("Error- File not found\n");

        system("pause");
    }

    while( fscanf(input, "%s", tempid) != -1)
    {
        if((tempid[0] == id[0])&&(tempid[1] == id[1])&&(tempid[2] == id[2])&&(tempid[3] == id[3])&&(tempid[4] == id[4])&&(tempid[5] == id[5])&&(tempid[6] == id[6])&&(tempid[7] == id[7])&&(tempid[8] == id[8])&&(tempid[9] == id[9]))
        {
            existance = 1;
            fscanf(input, "%s", student_f_name);
            fscanf(input, "%s", student_l_name);
        }
    }

    fclose(input);
    return existance;
}
int getGrades (char id[])
{
    int existance = 0;
    char tempid[8];
    int counter = 0;

    FILE* input;
    input = fopen("Grades.txt", "r");
    if(input == NULL)
    {
        printf("Error- File not found\n");

        system("pause");
    }
    while( fscanf(input, "%s", tempid) != -1)
    {
        if((tempid[0] == id[0])&&(tempid[1] == id[1])&&(tempid[2] == id[2])&&(tempid[3] == id[3])&&(tempid[4] == id[4])&&(tempid[5] == id[5])&&(tempid[6] == id[6])&&(tempid[7] == id[7])&&(tempid[8] == id[8])&&(tempid[9] == id[9]))
        {
            existance = 1;
            while(counter < 60)
            {
                fscanf(input, "%d", &grades[counter]);
                counter = counter + 1;
            }
        }
    }
    fclose(input);
    return existance;
}

int addGrades (char id[])
{
    FILE* input;
    FILE* output1;
    int counter = 0;
    char tempid[20];
    int tempgrade;
    int check = 0, check2 = 0;
    int existance = 0;


    input = fopen("Grades.txt", "r");
    if(input == NULL)
    {
        printf("Error- File not found\n");

        system("pause");
    }
    output1 = fopen("Temp.txt", "w");
    if(input == NULL)
    {
        printf("Error- File not found\n");

        system("pause");
    }
    while( fscanf(input, "%s", tempid) != -1)
    {
        check = 0;
        if((tempid[0] == id[0])&&(tempid[1] == id[1])&&(tempid[2] == id[2])&&(tempid[3] == id[3])&&(tempid[4] == id[4])&&(tempid[5] == id[5])&&(tempid[6] == id[6])&&(tempid[7] == id[7])&&(tempid[8] == id[8])&&(tempid[9] == id[9]))
        {
            fprintf(output1, "\n%s", tempid);
            counter = 0;
            check2 = 1;
            existance = 1;
            while(counter < 60)
            {
                fprintf(output1, " %d", grades[counter]);
                counter = counter + 1;
            }

        }
        else
        {

            fprintf(output1, "\n%s", tempid);
            check = 1;
            counter = 0;
            while(counter < 60)
            {
                fscanf(input, "%d", &tempgrade);
                fprintf(output1, " %d", tempgrade);
                counter = counter + 1;
            }
        }

        if(check == 0)
            {
                counter = 0;
                while(counter < 60)
                {
                    fscanf(input, "%d", &tempgrade);
                    counter = counter + 1;
                }
            }


    }

    if(check2 != 1)
    {
        fprintf(output1, "\n%s", id);
        counter = 0;
        while(counter < 60)
            {
                fprintf(output1, " %d", grades[counter]);
                counter = counter + 1;
            }
    }

    fclose(input);
    fclose(output1);

    output1 = fopen("Grades.txt", "w");
    if(input == NULL)
    {
        printf("Error- File not found\n");

        system("pause");
    }
    input = fopen("Temp.txt", "r");
    if(input == NULL)
    {
        printf("Error- File not found\n");

        system("pause");
    }
    while( fscanf(input, "%s", tempid) != -1)
    {
        fprintf(output1, "\n%s", tempid);
        counter = 0;
        while(counter < 60)
        {
            fscanf(input, "%d", &tempgrade);
            fprintf(output1, " %d", tempgrade);
            counter = counter + 1;
        }
    }

    fclose(input);
    fclose(output1);


    return existance;
}

void createStudent ( char id[], char fname[], char lname[])
{
    FILE* input;

    input = fopen("Student.txt", "a");
    if(input == NULL)
    {
        printf("Error- File not found\n");

        system("pause");
    }

    fprintf(input, "\n%s", id);
    fprintf(input, " %s", fname);
    fprintf(input, " %s", lname);

    fclose(input);
}

int getNumModules (void)
{
    int count = 0;
    int value;
    char rubbish[20];
    FILE* input;

    input = fopen("Module.txt", "r");
    if(input == NULL)
    {
        printf("Error- File not found\n");

        system("pause");
    }

    while( fscanf(input, "%s", rubbish) != -1)
    {
        fscanf(input, "%s", rubbish);
        fscanf(input, "%d", &value);
        credits[count] = value;
        count = count + 1;
    }

    return count;

}

//void create_module (char name[], char id[], int value)
void createModule (char name[], char id[], int value)
{

    FILE* input;

    input = fopen("Module.txt", "a");
    if(input == NULL)
    {
        printf("Error- File not found\n");

        system("pause");
    }

    fprintf(input, "\n%s", id);
    fprintf(input, " %s", name);
    fprintf(input, " %d", value);

    fclose(input);

}
